package top.ljlgithub.api.service.impl;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import top.ljlgithub.api.bean.*;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.mapper.GoodsMapper;
import top.ljlgithub.api.service.GoodsService;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/7 15:24
 * @Version 1.0
 **/
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @SneakyThrows
    @Override
    public GoodsPageBean getGoodsList(GoodsParam goodsParam) {
        if (goodsParam.getPagesize() == null || goodsParam.getPagenum() == null){
            throw new ParamException("参数错误");
        }
        GoodsPageBean goodsPageBean = new GoodsPageBean();
        Integer total = goodsMapper.getTotal(goodsParam.getQuery());
        goodsPageBean.setTotal(total);
        goodsPageBean.setPagenum(goodsParam.getPagenum());
        int startIndex = (goodsParam.getPagenum() - 1) * goodsParam.getPagesize();
        List<Goods> goodsList = goodsMapper.getGoodsList(goodsParam.getQuery(), startIndex, goodsParam.getPagesize());
        goodsPageBean.setGoods(goodsList);
        return goodsPageBean;
    }

    @Override
    public void deleteGoods(Integer id) {
        goodsMapper.deleteGoods(id);
    }

    @Override
    public GoodsInfo getGoodsInfo(Integer id) {
        GoodsInfo goodsInfo = goodsMapper.findGoodsById(id);
        List<Pic> picList = goodsMapper.getPicList(id);
        List<AttrGood> attrGoodList = goodsMapper.getAttrGoodList(id);
        goodsInfo.setPics(picList);
        goodsInfo.setAttrs(attrGoodList);
        String goods_cat = "";
        goods_cat += goodsInfo.getCat_one_id() + ",";
        goods_cat += goodsInfo.getCat_two_id() + ",";
        goods_cat += goodsInfo.getCat_three_id();
        goodsInfo.setGoods_cat(goods_cat);
        return goodsInfo;
    }

    @SneakyThrows
    @Override
    public GoodsInfo addGoods(AddGoods addGoods) {
        String[] split = addGoods.getGoods_cat().split(",");
        addGoods.setCat_one_id(Integer.parseInt(split[0]));
        addGoods.setCat_two_id(Integer.parseInt(split[1]));
        addGoods.setCat_three_id(Integer.parseInt(split[2]));
        addGoods.setCat_id(addGoods.getCat_three_id());
        addGoods.setUpd_time(new Date().getTime());
        addGoods.setAdd_time(new Date().getTime());
        goodsMapper.addGoods(addGoods);
        List<HashMap<String, String>> pics = addGoods.getPics();
        String path = ResourceUtils.getURL("classpath:").getPath();
        for (HashMap<String, String> pic : pics) {
            String p = pic.get("pic");
            handleUpload(path,p);
            String pics_big = "uploads/goodspics/big" + p;
            String pics_mid = "uploads/goodspics/mid" + p;
            String pics_sma = "uploads/goodspics/sma" + p;
            Pic pic1 = new Pic(null, addGoods.getGoods_id(), pics_big, pics_mid, pics_sma);
            goodsMapper.addPic(pic1);
        }
        List<GoodsAttr> attrs = addGoods.getAttrs();
        for (GoodsAttr attr : attrs) {
            AttrGood attrGood = new AttrGood();
            attrGood.setGoods_id(addGoods.getGoods_id());
            attrGood.setAttr_id(attr.getAttr_id());
            attrGood.setAttr_value(attr.getAttr_value());
            goodsMapper.addAttr(attrGood);
        }
        GoodsInfo goodsInfo = getGoodsInfo(addGoods.getGoods_id());
        return goodsInfo;
    }

    @Override
    public GoodsInfo updateGoods(AddGoods addGoods) {
        goodsMapper.updateGoods(addGoods);
        GoodsInfo goodsInfo = getGoodsInfo(addGoods.getGoods_id());
        return goodsInfo;
    }

    private void FileCopy(File srcFile,File destFile) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        fis = new FileInputStream(srcFile);
        //如果文件不存在则创建
        fos = new FileOutputStream(destFile);
        byte[] buffer = new byte[1024*1024];//1MB
        int readCount = 0;
        while ((readCount = fis.read(buffer)) != -1){
            fos.write(buffer,0,readCount);
        }
        fos.flush();
        if (fis != null) {
            fis.close();
        }
        if (fos != null){
            fos.close();
        }
        //删除源文件
        srcFile.delete();
    }

    private void handleUpload(String path,String tmp_uploads) throws IOException {
            File file = new File(path, "static"+tmp_uploads);
            if (file.exists()){
                File uploads_dir = new File(path, "static/uploads");
                //判断目录是否存在,不存在则创建该目录
                if (!uploads_dir.exists()) {
                    uploads_dir.mkdir();
                }
                //复制文件
                File destFile = new File(path, "static/uploads/"+file.getName());
                FileCopy(file,destFile);
            }
    }


}
